<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="" prop="nameCase">
                    <el-input class="w-[280px]" v-model="queryParams.nameCase" placeholder="案件名称"/>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="编号" prop="mediationNumber" min-width="40" />
                <el-table-column label="案件名称" prop="nameCase" min-width="150" />
                <el-table-column label="案件编号" prop="caseNumber" min-width="100" />
                <el-table-column label="调解员" prop="courtClerk" min-width="100" />
                <el-table-column label="案件类型" prop="caseType" min-width="80" >
                    <template #default="{ row }">
                        <dict-value :options="dictData.case_type" :value="row.caseType" />
                    </template>
                </el-table-column>
                <el-table-column label="调解结果" prop="mediationResult" min-width="80" >
                    <template #default="{ row }">
                        <dict-value :options="dictData.mediation_result" :value="row.mediationResult" />
                    </template>
                </el-table-column>
                <el-table-column label="受理时间" prop="receptionTimeStr" min-width="120" />
                <el-table-column label="完成时间" prop="completionTimeStr" min-width="120" />
                <el-table-column label="视频地址" prop="" min-width="80" align="center">
                    <template #default="{ row }">
                        <el-button v-if="row.video"
                            type="primary" link @click="handlePreview(row.video, 'video')">
                            查看
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="调解书 " prop="mediationDocument" min-width="80" align="center">
                    <template #default="{ row }">
                        <el-button v-if="row.video"
                            type="primary" link @click="handlePreview(row.mediationDocument, 'image')">
                            查看
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['record:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            详情
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
        <preview v-model="showPreview" :url="previewUrl" :type="type" />
    </div>
</template>
<script lang="ts" setup name="record">
import { recordDelete, recordLists } from '@/api/record'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {useDictData} from "@/hooks/useDictOptions";
import Preview from '@/components/material/preview.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    nameCase: '',
})
const showPreview = ref(false)
const previewUrl = ref('')
const type = ref('image')


const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: recordLists,
    params: queryParams
})

const { dictData } = useDictData<{
    case_type: any[],
    mediation_result: any[],
    yes1_no0: any[],
}>(['case_type', 'mediation_result', 'yes1_no0'])

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handlePreview = (url: string, typeStr: string) => {
    console.log(url)
    console.log(typeStr)
    type.value = typeStr
    previewUrl.value = url
    showPreview.value = true
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (mediationNumber: number) => {
    await feedback.confirm('确定要删除？')
    await recordDelete({ mediationNumber })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
